//
//  GraphViewCell.m
//  MonitoringNetwork
//
//  Created by admin on 2017/12/26.
//  Copyright © 2017年 wtmcxlm. All rights reserved.
//

#import "GraphViewCell.h"

#import "GraphView.h"
#import "NSString+StringSize.h"
@interface GraphViewCell()<UIScrollViewDelegate>
@property (nonatomic,strong)UILabel *titleLabel;
@property (nonatomic,strong)UIScrollView *baseScrollView;
@property (nonatomic,strong)UILabel *firstVerticallabel;
@end

@implementation GraphViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setUpUI];
    }
    return self;
}
- (void)setUpUI
{
    UIView *backView = [UIView new];
    backView.backgroundColor = [UIColor colorWithRed:0.97f green:0.97f blue:0.97f alpha:1.00f];
    [self addSubview:backView];
    [backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(self);
        make.height.mas_equalTo(11);
    }];
    
    _titleLabel = [UILabel LabelWithFont:NormalFontSize andColor:TitleColor andTextAlignment:left andString:@"行驶里程分布"];
    [self addSubview:_titleLabel];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).offset(24);
        make.right.mas_equalTo(self);
        make.top.mas_equalTo(self).offset(30);
    }];
    
    
 
    
}
#define POINT(_INDEX_) [(NSValue *)[pointArr objectAtIndex:_INDEX_] CGPointValue]
- (void)laodGraphWithDataArr:(NSArray *)arr andColor:(UIColor *)color
{
    CGSize tempSize = [@"00" sizeWithLabelWidth:100 font:[UIFont systemFontOfSize:NormalFontSize]];
    CGFloat tempHeight = tempSize.height*0.5;
    
    if (arr.count == 0) {
        UILabel *centerLabel = [UILabel LabelWithFont:BigFontSize andColor:TitleColor andTextAlignment:center andString:@"无统计数据"];
        [self addSubview:centerLabel];
        [centerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.centerY.mas_equalTo(self);
        }];
        
        return;
    }
    

    //竖向线
    UIBezierPath *linePath1 = [UIBezierPath bezierPath];
    [linePath1 moveToPoint:CGPointMake(173.0f/3, 188.0f/3)];
    [linePath1 addLineToPoint:CGPointMake(173.0f/3, 637/3.0f)];
    CAShapeLayer *lineLayer1 = [CAShapeLayer layer];
    lineLayer1.lineWidth = 0.5f;
    lineLayer1.strokeColor = [UIColor colorWithRed:0.20f green:0.20f blue:0.20f alpha:1.00f].CGColor;
    lineLayer1.path = linePath1.CGPath;
    lineLayer1.fillColor = nil;
    [self.layer addSublayer:lineLayer1];
    
   
    int maxNum = 0;
    int totalNum = 0;
    for (int i=0; i<arr.count; i++) {
        NSDictionary *dic = arr[i];
        totalNum += [dic[@"value"] intValue];
        if (maxNum<[dic[@"value"] intValue]) {
            maxNum = [dic[@"value"] intValue];
        }
    }
    int eachIntValu = maxNum/(4.f);
    NSString *str  = [NSString stringWithFormat:@"%d",eachIntValu];
    int eachValu =  ceil(maxNum*1.0f/(4.f)/pow(10,str.length-1))*pow(10,str.length-1);
    
    CGFloat lineVerticalLength = 261-143/3.0f - 188.0f/3;
    
    CGFloat verticalLength = lineVerticalLength/(4+1.f);
    
    CGFloat x = 173.0f/3 ;
    CGFloat y = 261-143/3.0f;
    
    //竖向label
    for (int i=0; i<5; i++) {
        UILabel *verticallabel = [UILabel LabelWithFont:NormalFontSize andColor:TitleColor andTextAlignment:right andString:[NSString stringWithFormat:@"%d",eachValu*i]];
        [self addSubview:verticallabel];
        [verticallabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self.mas_left).offset(x-6);
            make.bottom.mas_equalTo(self.mas_top).offset(y-verticalLength*(i));
        }];
    }
    
    //创建滑动视图
    _baseScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(173.0f/3, 0, kScreenWidth - 173.0f/3 ,548.f/3+188.0f/3)];
    [self addSubview:_baseScrollView];
    _baseScrollView.contentSize = CGSizeMake(60*arr.count, 548.f/3+188.0f/3);
    _baseScrollView.bounces = NO;
    _baseScrollView.showsHorizontalScrollIndicator = NO;
    _baseScrollView.delegate = self;
    //创建背景
    UIView *canv = [[UIView alloc] initWithFrame:CGRectMake(0, 0, (arr.count+1)*60,548.f/3+188.0f/3)];
    [_baseScrollView addSubview:canv];

    
    //横向线
    
    UIBezierPath *linePath2 = [UIBezierPath bezierPath];
    [linePath2 moveToPoint:CGPointMake(0, 188.0f/3)];
    [linePath2 addLineToPoint:CGPointMake(0, 637/3.0f)];
    [linePath2 addLineToPoint:CGPointMake((arr.count)*60, 637/3.0f)];
    CAShapeLayer *lineLayer2 = [CAShapeLayer layer];
    lineLayer2.lineWidth = 0.5f;
    lineLayer2.strokeColor = [UIColor colorWithRed:0.20f green:0.20f blue:0.20f alpha:1.00f].CGColor;
    lineLayer2.path = linePath2.CGPath;
    lineLayer2.fillColor = nil;
    [canv.layer addSublayer:lineLayer2];
    
    
    //横向label
    NSDictionary *dict = arr[0];
    _firstVerticallabel = [UILabel LabelWithFont:NormalFontSize andColor:TitleColor andTextAlignment:right andString:[NSString stringWithFormat:@"%@",dict[@"key"]]];
    
    [self addSubview:_firstVerticallabel];
    [_firstVerticallabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(canv.mas_left);
        make.bottom.mas_equalTo(canv.mas_bottom);
    }];
    for (int i=1; i<arr.count; i++) {
        NSDictionary *dic = arr[i];
        UILabel *verticallabel = [UILabel LabelWithFont:NormalFontSize andColor:TitleColor andTextAlignment:right andString:[NSString stringWithFormat:@"%@",dic[@"key"]]];
        [canv addSubview:verticallabel];
        [verticallabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(canv.mas_left).offset(60*i);
            make.bottom.mas_equalTo(canv.mas_bottom);
        }];
    }
    
    
    //创建点数组
    NSMutableArray *pointArr = [NSMutableArray array];
    NSMutableArray *tempArr = [NSMutableArray array];
    for (int i=0; i<arr.count; i++) {
        NSDictionary *dic = arr[i];
        int num = [dic[@"value"] intValue];
        CGPoint point = CGPointMake( 60*i ,637/3.0f- num*1.0f/eachValu*verticalLength-tempHeight);
//        NSLog(@"%@",NSStringFromCGPoint(point));
        [tempArr addObject:[NSValue valueWithCGPoint:CGPointMake(point.x, point.y)]];
    }
    
    pointArr = tempArr;
    UIBezierPath *smoothedPath = [UIBezierPath bezierPath];
    
    [pointArr insertObject:[pointArr objectAtIndex:0] atIndex:0];
    [pointArr addObject:[pointArr lastObject]];
    [smoothedPath moveToPoint:POINT(0)];
    
    for (NSUInteger index = 1; index < pointArr.count - 2; index++) {
        CGPoint p0 = POINT(index - 1);
        CGPoint p1 = POINT(index);
        CGPoint p2 = POINT(index + 1);
        CGPoint p3 = POINT(index + 2);
        for (int i = 1; i < 1000; i++) {
            float t = (float) i * (1.0f / (float) 1000);
            float tt = t * t;
            float ttt = tt * t;
            CGPoint pi;
            pi.x = 0.5 * (2*p1.x+(p2.x-p0.x)*t + (2*p0.x-5*p1.x+4*p2.x-p3.x)*tt + (3*p1.x-p0.x-3*p2.x+p3.x)*ttt);
            pi.y = 0.5 * (2*p1.y+(p2.y-p0.y)*t + (2*p0.y-5*p1.y+4*p2.y-p3.y)*tt + (3*p1.y-p0.y-3*p2.y+p3.y)*ttt);
            [smoothedPath addLineToPoint:pi];
        }
        [smoothedPath addLineToPoint:p2];
    }
    [smoothedPath addLineToPoint:POINT(pointArr.count - 1)];
    CAShapeLayer *shadeLayer = [CAShapeLayer new];
    shadeLayer.path = smoothedPath.CGPath;
    shadeLayer.strokeColor = color.CGColor;
    shadeLayer.fillColor = [[UIColor clearColor] CGColor];
    shadeLayer.lineWidth = 4.f/3;
    shadeLayer.lineCap = kCALineCapRound;
    shadeLayer.lineJoin = kCALineJoinRound;
    [canv.layer addSublayer:shadeLayer];
    
    //填充
    UIBezierPath *shapePath = [UIBezierPath bezierPath];
    [shapePath moveToPoint:POINT(0)];
    for (NSUInteger index = 1; index < pointArr.count - 2; index++) {
        CGPoint p0 = POINT(index - 1);
        CGPoint p1 = POINT(index);
        CGPoint p2 = POINT(index + 1);
        CGPoint p3 = POINT(index + 2);
        for (int i = 1; i < 1000; i++) {
            float t = (float) i * (1.0f / (float) 1000);
            float tt = t * t;
            float ttt = tt * t;
            CGPoint pi;
            pi.x = 0.5 * (2*p1.x+(p2.x-p0.x)*t + (2*p0.x-5*p1.x+4*p2.x-p3.x)*tt + (3*p1.x-p0.x-3*p2.x+p3.x)*ttt);
            pi.y = 0.5 * (2*p1.y+(p2.y-p0.y)*t + (2*p0.y-5*p1.y+4*p2.y-p3.y)*tt + (3*p1.y-p0.y-3*p2.y+p3.y)*ttt);
            [shapePath addLineToPoint:pi];
        }
        [shapePath addLineToPoint:p2];
    }
    [shapePath addLineToPoint:POINT(pointArr.count-1)];
    [shapePath addLineToPoint:CGPointMake((arr.count-1)*60, 637/3.0f)];
    [shapePath addLineToPoint:CGPointMake(0, 637/3.0f)];
    [shapePath addLineToPoint:POINT(0)];
    CAShapeLayer *dimLayer = [CAShapeLayer new];
    dimLayer.path = shapePath.CGPath;
    dimLayer.strokeColor = [UIColor clearColor].CGColor;
    dimLayer.fillColor = [UIColor colorWithRed:0.21f green:0.50f blue:0.90f alpha:0.2f].CGColor;
    dimLayer.lineWidth = 1;
    dimLayer.lineCap = kCALineCapRound;
    dimLayer.lineJoin = kCALineJoinRound;
    [canv.layer addSublayer:dimLayer];
    
    GraphView *graphView = [GraphView new];
    graphView.tipTitle = _tipTitle;
    graphView.dataArray = arr;
    graphView.lineColor = [UIColor colorWithRed:0.21f green:0.50f blue:0.90f alpha:1.0f];
    graphView.pointArray = tempArr.copy;
    [canv addSubview:graphView];
    [graphView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(canv);
        make.bottom.mas_equalTo(canv.mas_top).offset(637/3.0f+1);
    }];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat W = scrollView.contentOffset.x;
    if (W) {
        _firstVerticallabel.hidden = YES;
    }else{
        _firstVerticallabel.hidden = NO;
    }
}

- (void)drawRect:(CGRect)rect
{
    
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    _titleLabel.text = _title;
}





@end
